package com.hopital5.zhangyz.vo.response;

import com.google.common.collect.ImmutableMap;
import com.hopital5.zhangyz.model.Activity;

import com.hopital5.zhangyz.model.ActivityActivityItem;
import lombok.Builder;
import lombok.Data;

import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.Collectors;

@Data
@Builder
public class ActivityVoResponse {
    private Long id;
    private Date beginDate;
    private Date endDate;
    private String name;
    private List<Map<String, String>> activityActivityItemsHeader;
    private List<Map<Object, Object>> activityActivityItems;

    public static ActivityVoResponse build(Activity activity) {
        Map<PeopleVoResponse, List<ActivityActivityItemVoResponse>> peopleActivityActivityItem = activity.getActivityActivityItems().stream()
                .map(ActivityActivityItemVoResponse::build).collect(Collectors.groupingBy(ActivityActivityItemVoResponse::getPeople));
        List<Map<String, String>> activityActivityItemsHeader = peopleActivityActivityItem.entrySet().stream().findFirst().map(peopleVoResponseListEntry -> {
            List<Map<String, String>> result = new ArrayList<>();
            result.add(ImmutableMap.of("name", "ID", "value", "id"));
            result.add(ImmutableMap.of("name", "学号", "value", "number"));
            result.add(ImmutableMap.of("name", "姓名", "value", "number"));
            peopleVoResponseListEntry.getValue().stream().sorted(Comparator.comparing(activityActivityItemVoResponse -> activityActivityItemVoResponse.getActivityItem().getId()))
                    .forEachOrdered(activityActivityItemVoResponse -> result.add(ImmutableMap.of(
                            "name", String.format("%s(%s)", activityActivityItemVoResponse.getActivityItem().getName(), activityActivityItemVoResponse.getActivityItem().getUnit()),
                            "value", String.format("ActivityItem_Id_%s", activityActivityItemVoResponse.getActivityItem().getId()))));
            return result;
        }).orElse(null);
        List<Map<Object, Object>> activityActivityItems = peopleActivityActivityItem.entrySet().stream().map(peopleVoResponseListEntry -> {
            Map<Object, Object> result = new HashMap<>();
            result.put("id", peopleVoResponseListEntry.getKey().getId());
            result.put("number", peopleVoResponseListEntry.getKey().getNumber());
            result.put("name", peopleVoResponseListEntry.getKey().getName());
            peopleVoResponseListEntry.getValue().stream().sorted(Comparator.comparing(activityActivityItemVoResponse -> activityActivityItemVoResponse.getActivityItem().getId()))
                    .forEachOrdered(activityActivityItemVoResponse -> result.put(String.format("ActivityItem_Id_%s", activityActivityItemVoResponse.getActivityItem().getId()), activityActivityItemVoResponse.getNumber()));
            return result;
        }).collect(Collectors.toList());
        ActivityVoResponse activityVoResponse = ActivityVoResponse.builder()
                .id(activity.getId())
                .beginDate(activity.getBeginDate())
                .endDate(activity.getEndDate())
                .name(activity.getName())
                .activityActivityItemsHeader(activityActivityItemsHeader)
                .activityActivityItems(activityActivityItems)
                .build();
        return activityVoResponse;
    }

}
